import axios from 'axios'
import { defineStore } from 'pinia'
import route from '@/router/index.ts'

export const useUserStore = defineStore('user', {
  state: () => {
    return { username: '',
             password: '' }
  },
  getters: {
    getUsername(): string {
        return this.username
      },
      getPassword(): string {
        return this.password
      }
  },
  actions: {
    async setUsername(username : string) {
        await axios.post(`http://localhost:8080/users/` + username).then(response =>{
            // data.value = response.data
            // console.log(response.data[0].username)
            localStorage.setItem('username', response.data[0].username);  
            localStorage.setItem('password', response.data[0].password);  
         })
      
        this.username = username
        route.push('/')
      },

      setPassword(password: string) {
        this.password = password
      }  
  },
})